<!-- 新增编辑车辆 -->
<template>
  <div class="app-container">
    <el-dialog
      title="新增车辆"
      :visible="dialogShow"
      width="39%"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item prop="truckTypeId" label="车辆类型">
          <el-select
            v-model="form.truckTypeId"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in carTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="licensePlate" label="车牌号码">
          <el-input
            v-model="form.licensePlate"
            placeholder="请输入车牌号码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deviceGpsId" label="GPS设备ID">
          <el-input
            v-model="form.deviceGpsId"
            placeholder="请输入GPS设备ID"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button class="confirmation" type="warning" size="medium" @click="validateRequests">
            确认
          </el-button>
          <el-button class="cancel" size="medium" @click="close">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    carTypeList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        licensePlate: '', // 车牌号
        deviceGpsId: '', // GPS
        truckTypeId: null // 车辆类型ID
      },
      rules: {
        truckTypeId: [
          { required: true, message: '请选择车辆类型', trigger: 'blur' }
        ],
        licensePlate: [
          { required: true, message: '请填入车牌号', trigger: 'blur' },
          { pattern: /^[\u4e00-\u9fa5][A-Z][0-9]{5,6}$/, message: '车牌号不符合规范，规范示例：京A12345', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: 'GPS设备ID不可以为空', trigger: 'blur' },
          { pattern: /^[A-Za-z0-9]{1,20}$/, message: 'GPS设备ID不符合规范，规范示例：0xABC123DEF', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 弹窗关闭逻辑
    close() {
      console.log(123)
      this.$refs.form.resetFields()
      this.$emit('update:dialogShow', false)
    },
    // 点击确认二次校验并提交新增请求
    validateRequests() {
      this.$refs.form.validate(async isOK => {
        if (isOK) {
          // console.log(this.form)
          await truckTypeAdd(this.form)
          this.$emit('add')
          this.close()
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding: 40px 50px 0 20px !important;
}
::v-deep .el-form-item__label {
  font-weight: 400;
}
.app-container {
  ::v-deep .el-dialog__footer {
    padding: 14px 30px 38px 30px;
  }
  .confirmation {
    width: 90px;
    height: 40px;
  }
  .cancel {
    margin-left: 20px;
    width: 90px;
    height: 40px;
  }
}
</style>
